//
//  RBStockAddOrderSubmitVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/17.
//

#import "RBStockAddOrderSubmitVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "RBTextField.h"
#import "RBTextView.h"
#import "RBStockAddOrderSucVC.h"
#import "RBAddressListResponse.h"
#import "RBAddresssListVC.h"
#import "RBProcessOrderVC.h"

@interface RBStockAddOrderSubmitVC ()

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) RBTextView *markF;

@property (nonatomic, strong) RBAddressListModel *addressModel;

@end

@implementation RBStockAddOrderSubmitVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"加工下单";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        [view rbAddTapGesWithTarget:self action:@selector(clickChooseAddress)];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:@"配送至"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(17) bottom:Zoom(17) left:0 right:0 centerY:0];
            [view h_view_addView:[RanTool imgViewWithMode:1 radius:0 imgName:@"mine_arrow_right"] withHeight:Zoom(12) width:Zoom(7) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:0 centerY:YES];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:0 text:@"请选择收货地址"];
            view.tag = 200;
            view.numberOfLines = 0;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textGrayColor] align:0 text:@""];
            view.numberOfLines = 0;
            view.tag = 201;
            view.hidden = YES;
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(14) bottom:Zoom(17) left:Zoom(13) right:Zoom(13) centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        
        [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:@"订单备注"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(16) bottom:0 left:Zoom(13) right:0 centerX:0];
        
        [view view_addView:({
            UIView *view = UIView.new;
            [view rbSetCornerRadius:Zoom(5)];
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            [view view_addView:({
                RBTextView *view = RBTextView.new;
                view.backgroundColor = UIColor.clearColor;
                view.font = [UIFont systemFontOfSize:Zoom(13)];
                view.textColor = [RBThemeConfig textBlackColor];
                view.placeHolder = @"请填写订单备注";
                view.placeHolderFontSize = Zoom(13);
                view;
            }) withHeight:Zoom(90) width:CGFLOAT_MIN top:Zoom(13)-7 bottom:Zoom(13)-7 left:Zoom(13)-3 right:Zoom(13)-3 centerX:NO];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(16) bottom:Zoom(18) left:Zoom(13) right:Zoom(13) centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(15) right:Zoom(15) centerX:0];
}
- (void)appendData {
    UILabel *nameLabel = [self.view viewWithTag:200];
    UILabel *addressLabel = [self.view viewWithTag:201];
    if (self.addressModel.name && self.addressModel.name.length > 0) {
        nameLabel.text = [NSString stringWithFormat:@"%@　%@", self.addressModel.name, self.addressModel.mobile];
        addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@", self.addressModel.province, self.addressModel.city, self.addressModel.area, self.addressModel.address];
        addressLabel.hidden = NO;
    }
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        
    } path:_Address_list post:YES log:YES responseClass:RBAddressListResponse.class desView:self.view showHud:NO showPlaceHolder:NO sucBlock:^(RBAddressListResponse *data) {
        weakSelf.addressModel = data.data.firstObject;
        [weakSelf appendData];
    } failBlock:^(RBAddressListModel *data) {
    } reloadBlock:nil];
}

#pragma mark - Event

- (void)clickChooseAddress {
    RBAddresssListVC *vc = RBAddresssListVC.new;
    LCWeakSelf;
    vc.selectBlock = ^(RBAddressListModel *model) {
        weakSelf.addressModel = model;
        [weakSelf appendData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)confirmBtnTapped
{
    if (self.addressModel.name && self.addressModel.name.length > 0) {
        LCWeakSelf;
        [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
            dicM[@"parent_id"] = self.submitModel.parent_id;
            dicM[@"user_id"] = self.submitModel.user_id;
            dicM[@"order_type"] = self.submitModel.order_type;
            dicM[@"mail_type"] = self.submitModel.mail_type;
            dicM[@"user_address_id"] = self.addressModel.add_id;
            dicM[@"total_num"] = self.submitModel.total_num;
            dicM[@"spec_key"] = self.submitModel.spec_key;
            dicM[@"institution_name"] = self.submitModel.institution_name;
            dicM[@"institution_address"] = self.submitModel.institution_address;
            dicM[@"doctor_name"] = self.submitModel.doctor_name;
            dicM[@"doctor_mobile"] = self.submitModel.doctor_mobile;
            dicM[@"patient_name"] = self.submitModel.patient_name;
            dicM[@"patient_sex"] = self.submitModel.patient_sex;
            dicM[@"postscript"] = self.submitModel.postscript;
            dicM[@"picarr"] = self.submitModel.picarr;
            dicM[@"remark"] = self.markF.text;
        } path:_Order_sub_create post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
            if (data.succeed) {
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                RBProcessOrderVC *vc = RBProcessOrderVC.new;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        } failBlock:^(RBResponse *data) {
            [weakSelf.view showTip:data.message completion:nil];
        } reloadBlock:nil];
    } else {
        [self.view showTip:@"请选择地址" completion:nil];
    }
}

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)bottomView
{
    if (!_bottomView)
    {
        self.bottomView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(60) + kRBSafeBottomHeight);
            }];
            
            UIStackView *stackView = UIStackView.new;
            stackView.spacing = Zoom(20);
            stackView.axis = UILayoutConstraintAxisHorizontal;
            stackView.distribution = UIStackViewDistributionFillEqually;
            [view addSubview:stackView];
            [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(Zoom(15));
                make.right.mas_equalTo(-Zoom(15));
                make.top.mas_equalTo(Zoom(10));
                make.height.mas_equalTo(Zoom(40));
            }];
            [stackView addArrangedSubview:({
                UILabel *view = [self btnWithTitle:@"确认提交" type:1];
                [view rbAddTapGesWithTarget:self action:@selector(confirmBtnTapped)];
                view;
            })];
            view;
        });
    }
    return _bottomView;
}

- (UILabel *)btnWithTitle: (NSString *)title type: (NSInteger)type
{
    if (type == 0)
    {
        UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textLightBlackColor] align:1 text:title bgColor:UIColor.whiteColor radius:Zoom(20)];
        [view rbSetBorderWitdh:1 borderColor:[RBThemeConfig textGrayColor]];
        return view;
    }
    else
    {
        return [RanTool labelWithFontSize:Zoom(13) weight:1 color:UIColor.whiteColor align:1 text:title bgColor:[RBThemeConfig buttonBlueColor] radius:Zoom(20)];
    }
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.bottom.mas_equalTo(self.bottomView.mas_top);
            }];
            view;
        });
    }
    return _scrollView;
}

@end
